part of '../repository.dart';

const _kRefreshTokenValueKey = 'value';
const _kRefreshTokenUserIdKey = 'user_id';
const _kRefreshTokenUserRoleKey = 'user_role';
const _kRefreshTokenExpirationTimeKey = 'expiration_time';

class _RefreshTokenDto extends Equatable {
  const _RefreshTokenDto._internal({
    required this.value,
    required this.userId,
    required this.userRoleDto,
    required this.expirationTimeDto,
  });

  final String value;
  final String userId;
  final _UserRoleDto userRoleDto;
  final _TimeDto expirationTimeDto;

  @override
  List<Object?> get props {
    return [
      value,
      userId,
      userRoleDto,
      expirationTimeDto,
    ];
  }

  factory _RefreshTokenDto.fromJson(Map<String, dynamic> json) {
    final userRoleString = json[_kRefreshTokenUserRoleKey] as String;
    final userRoleDto = _UserRoleDto.fromString(userRoleString);

    final expirationTimeString = json[_kRefreshTokenExpirationTimeKey] as String;
    final expirationTimeDto = _TimeDto.fromString(expirationTimeString);

    return _RefreshTokenDto._internal(
      value: json[_kRefreshTokenValueKey],
      userId: json[_kRefreshTokenUserIdKey],
      userRoleDto: userRoleDto,
      expirationTimeDto: expirationTimeDto,
    );
  }

  factory _RefreshTokenDto.fromEntity(RefreshToken refreshToken) {
    final userRoleDto = _UserRoleDto.fromEntity(refreshToken.userRole);
    final expirationTimeDto = _TimeDto.fromEntity(refreshToken.expirationTime);

    return _RefreshTokenDto._internal(
      value: refreshToken.value,
      userId: refreshToken.userId,
      userRoleDto: userRoleDto,
      expirationTimeDto: expirationTimeDto,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      _kRefreshTokenValueKey: value,
      _kRefreshTokenUserIdKey: userId,
      _kRefreshTokenUserRoleKey: userRoleDto.toString(),
      _kRefreshTokenExpirationTimeKey: expirationTimeDto.toString(),
    };
  }

  RefreshToken toEntity() {
    return RefreshToken(
      value: value,
      userId: userId,
      userRole: userRoleDto.toEntity(),
      expirationTime: expirationTimeDto.toEntity(),
    );
  }
}
